#include "os_sys.h"
#include "work.h"

#include "switch_driver.h"
#include "switch_bsp.h"


#define SWITCH_TASK_TICK (111)


struct SWITCH_TASK_S
{
    SysTick_t switch_tick;

};

static struct SWITCH_TASK_S switch_task={.switch_tick=0};

u32 switch_test=0;
u8 test_status[4];

static void switch_run(void)
{
    if (0 == systick_get_difference_assign(&switch_task.switch_tick, SWITCH_TASK_TICK))
        return;

    switch (switch_test)
    {
    case 0x01:
        test_status[0] = switch_status(SWITCH_F_DARK);
        test_status[1] = switch_status(SWITCH_F_LIGHT);
        break;
    case 0x02:
        switch_off(SWITCH_PUL_EN);
    break;
    case 0x03:
        switch_on(SWITCH_PUL_EN);
    break;
    case 0x04:
        switch_pwm(SWITCH_PUL_EN,Tnms(2000),Tnms(2000));
        switch_pwm(SWITCH_DIR_EN,Tnms(1000),Tnms(1000));
    break;
    default:
        break;
    }
    switch_test=0;
}
static void switch_main_run(void)
{
    switch (sys_task_state_get(TASKID_SWITCH))
    {
        case TASK_STAT_FREE:
        case TASK_STAT_POWER_ON:
        case TASK_STAT_INITIAL:
            sys_task_state_set(TASKID_SWITCH, TASK_STAT_RUN);
            break;
        case TASK_STAT_RUN:
            switch_run();
            break;
        case TASK_STAT_UNINITIAL:
            sys_task_state_set(TASKID_SWITCH, TASK_STAT_POWER_OFF);
            break;
        case TASK_STAT_POWER_OFF:
        case TASK_STAT_SLEEP:

            break;
        default:
            sys_task_state_set(TASKID_SWITCH, TASK_STAT_POWER_ON);
            break;
    }

}
static void switch_peripheral_sleep(void)
{}


static void switch_sessage_process(E_DATAID DatID, const unsigned int data)
{
    switch (DatID)
    {
        case DATID_STATUS_LED_ON_OFF:
        if(0==data)
            led_off(STATUS_LED);
        else if(1==data)
            led_on(STATUS_LED);
        break;
        case DATID_STATUS_LED_BLINK:
        {
            u32 on_ms , off_ms;
            MSG_D(on_ms,off_ms);
            led_pwm(STATUS_LED,on_ms, off_ms);
        }
        break;

        default:
            break;
    }
}

static void switch_variable_init(void)
{
    switch_init();
}

//CONSTRUCTOR_DEFAUTL()
void switch_task_create(void)
{
    struct task_s task = TASK_DEFAULT_INIT();

    task.ID 		= TASKID_SWITCH;
    task.state		= TASK_STAT_FREE;
    task.TaskTime	= 0;
    task.run 		= switch_main_run;
    task.sleep_init = switch_peripheral_sleep;
    task.msg_process= switch_sessage_process;
    task.task_tick 	= NULL;
    task.is_used 	= 1;
	task.task_init 	= switch_variable_init;
    sys_task_add(&task);
}


